const { BaseCommand } = require("./base");
const { getDataByKey } = require("./share_data");

class List2DictCommand extends BaseCommand {
    async execute() {
        const data = this.selfData;
        const content = this.content;
        const keyIndex = data.key_index === undefined ? 0 : data.key_index;
        const valueIndex =
            data.value_index === undefined ? 1 : data.value_index;
        const value_self = data.value_self;
        const result = {};
        for (let i = 0; i < content.length; i++) {
            const v = content[i];
            if (value_self) {
                result[getDataByKey(`${keyIndex}`, v)] = v;
            } else if (Array.isArray(valueIndex)) {
                result[getDataByKey(`${keyIndex}`, v)] = valueIndex.map((i) =>
                    getDataByKey(`${v[i]}`, v),
                );
            } else {
                result[getDataByKey(`${keyIndex}`, v)] = getDataByKey(
                    `${valueIndex}`,
                    v,
                );
            }
        }
        return result;
    }

    getRequireContent() {
        return true;
    }
}

module.exports = {
    List2DictCommand,
};
